package io.bdmc.modules.api.controller;


import io.bdmc.common.utils.DateUtils;
import io.bdmc.common.utils.R;
import io.bdmc.modules.api.annotation.AuthIgnore;
import io.bdmc.modules.api.annotation.LoginUser;
import io.bdmc.modules.api.entity.TokenEntity;
import io.bdmc.modules.api.entity.UserEntity;
import io.bdmc.modules.bss.entity.BarDev;
import io.bdmc.modules.bss.entity.BarRealtime;
import io.bdmc.modules.bss.service.BarDevService;
import io.bdmc.modules.bss.service.BarRealtimeService;

import java.util.HashMap;
import java.util.Map;

import com.aliyun.oss.common.utils.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * API测试接口
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-23 15:47
 */
@RestController
@RequestMapping("/api")
public class ApiTestController {

    @Autowired
    private BarDevService devSvs;
    @Autowired
    private BarRealtimeService realSvs;
    /**
     * 获取用户信息
     */
    @GetMapping("userInfo")
    public R userInfo(@LoginUser UserEntity user){
        return R.ok().put("user", user);
    }

    /**
     * 获取用户ID
     */
    @GetMapping("userId")
    public R userInfo(@RequestAttribute("userId") Integer userId){
        return R.ok().put("userId", userId);
    }

    /**
     * 忽略Token验证测试
     */
    @AuthIgnore
    @GetMapping("notToken")
    public R notToken(){
        return R.ok().put("msg", "无需token也能访问。。。");
    }

    /**
     * 接收JSON数据
     */
    @PostMapping("jsonData")
    public R jsonData(@LoginUser UserEntity user, @RequestBody TokenEntity token){
        return R.ok().put("user", user).put("token", token);
    }

    @AuthIgnore
    @PostMapping("latlng")
    public R getLatLng(@RequestBody(required = false) Map<String, String> req){
        String sim = req.get("sim");
        if(StringUtils.isEmpty("sim")){
            return R.error(-1,"sim为空");
        }
        QueryWrapper<BarRealtime> w1 = new QueryWrapper<>();
        w1.eq("sim", sim);
        BarRealtime r = realSvs.getOne(w1);
        if(r==null){
            return R.error(-1,"该设备无实时经纬度信息");
        }
        HashMap<String,String> map = new HashMap<>();
        map.put("lat", r.getLat());
        map.put("lng", r.getLng());
        map.put("high", r.getHigh());
        map.put("dataTime", DateUtils.formatLocalDateTimeDefault( r.getDataTime()));
        return R.ok().putData(map);
    }
}
